package com.hao.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hao.commonutils.R;
import com.hao.eduservice.entity.EduCourse;
import com.hao.eduservice.entity.vo.CourseInfoVo;
import com.hao.eduservice.entity.vo.CoursePublishVo;
import com.hao.eduservice.entity.vo.CourseQuery;
import com.hao.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author hao
 * @since 2020-12-02
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/course")
public class EduCourseController {


    @Autowired
    private EduCourseService courseService;

    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id = courseService.saveCourseInfo(courseInfoVo);

        return R.ok().data("id",id);
    }

    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);

        return R.ok();
    }

    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId){
        CoursePublishVo vo = courseService.getPublishCourseInfo(courseId);
        return R.ok().data("publishCourse",vo);
    }


    /**
     * 发布课程
     *  course.setStatus("Normal");
     * @param courseId 进程id
     * @return {@link R}
     */
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        EduCourse course = courseService.getById(courseId);
        course.setStatus("Normal");
        boolean b = courseService.updateById(course);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("course/{current}/{limit}")
    public R courseList(@PathVariable long current, @PathVariable long limit,
                        @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> pageTeacher = new Page<>(current,limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        if(!StringUtils.isEmpty(title)) {
            //构建条件
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)) {
            //构建条件
            wrapper.eq("status",status);
        }
        courseService.page(pageTeacher,wrapper);
        //总记录数
        long total = pageTeacher.getTotal();
        //数据list集合
        List<EduCourse> list = pageTeacher.getRecords();
        return R.ok().data("total",total).data("list",list);
    }

    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.deleteCourse(courseId);
        return R.ok();
    }

}

